home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
4_7.lha
/
4_7
/
4_7e.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
896b
|
40 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
include <tnode.h>
include <string.h>
include <error.h>
/ insert a new node to the left, right
/ or at the node pointed to by the treeheadptr
oid addnode(tree treeheadptr, char *nword)
// a leaf found, enter the node here
if (!*treeheadptr)
{
tnode *n = new tnode;
*treeheadptr = n;
n->left = n->right = 0;
n->count = 1;
n->tword = new char[strlen(nword) + 1];
strcpy(n->tword, nword);
return;
}
// check the name
int cmp = strcmp(nword, (*treeheadptr)->tword);
// enter in the left subtree
if (cmp < 0)
addnode(&(*treeheadptr)->left, nword);
// enter in right subtree
else if (cmp > 0)
addnode(&(*treeheadptr)->right, nword);
// equal, increment the reference count
else /* if (cmp == 0) */
(*treeheadptr)->count++;